package com.wj.console.graphql.service;

import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import graphql.GraphQL;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;

public class TestGraphql {

	public static void main(String[] args) {
		lambdaTest1();

	}
	
	public static void lambdaTest1(){
		List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
		Collections.sort(names, (a,b) -> b.compareTo(a));
		names.stream().forEach(System.out::println);
	}
	public static void test2(){
		GraphQLSchema schema = new GraphSchema().getSchema();

        String query1 = "{users(page:2,size:5,name:\"john\") {id,sex,name,pic}}";
        String query2 = "{user(id:6) {id,sex,name,pic}}";
        String query3 = "{user(id:6) {id,sex,name,pic},users(page:2,size:5,name:\"john\") {id,sex,name,pic}}";

        Map<String, Object> result1 = (Map<String, Object>) new GraphQL(schema).execute(query1).getData();
        Map<String, Object> result2 = (Map<String, Object>) new GraphQL(schema).execute(query2).getData();
        Map<String, Object> result3 = (Map<String, Object>) new GraphQL(schema).execute(query3).getData();

        // 查询用户列表
        System.out.println(result1);
        // 查询单个用户
        System.out.println(result2);
        // 单个用户、跟用户列表一起查
        System.out.println(result3);

	}
	
	public static void test1(){
		GraphQLObjectType queryType = newObject().name("helloWorldQuery")
				.field(newFieldDefinition().type(GraphQLString).name("hello").staticValue("world").build()).build();
		GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).build();
		Map<String, Object> result = (Map<String, Object>) new GraphQL(schema).execute("{hello}").getData();
		System.out.println(result);
		// Prints: {hello=world}
	}
}
